import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Req,
  SetMetadata,
  Query,
  DefaultValuePipe,
  ParseIntPipe,
  UploadedFile,
  UseInterceptors
} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { ReflectMetadataKeys } from '@/common/enum/ReflectMetadataKeys';
import {
  ApiOperation,
  ApiParam,
  ApiQuery,
  ApiResponse,
  ApiTags
} from '@nestjs/swagger';
import { User } from './entities/user.entity';
import { UpdatePasswordUserDto } from './dto/update-password-user.dto';
import { Public } from '@/common';
import { userQueryVo } from '@/common/interface';
import { AttachementSize } from '../attachement/pipe/attachement-size.pipe';
import { FileInterceptor } from '@nestjs/platform-express';
import { Attachement } from '../attachement/entities/attachement.entity';

@ApiTags('用户管理')
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Public()
  @ApiOperation({ summary: '创建用户' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '创建用户')
  @Post()
  create(@Body() createUserDto: CreateUserDto, @Req() req: any) {
    return this.userService.create(createUserDto);
  }

  @ApiOperation({ summary: '用户列表' })
  @ApiResponse({ status: 200, description: '200', type: [User] })
  @ApiQuery({
    name: 'username',
    required: false,
    description: '登录账号',
    type: String,
    example: 'admin'
  })
  @ApiQuery({
    name: 'queryDate',
    required: false,
    description: '查询时间',
    type: Array
  })
  @ApiQuery({
    name: 'page',
    required: false,
    description: '第几页',
    type: Number,
    example: 1
  })
  @ApiQuery({
    name: 'limit',
    required: false,
    description: '每页显示数',
    type: Number,
    example: 10
  })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '用户列表')
  @Get()
  findAll(
    @Query() query?: userQueryVo,
    @Query('page', new DefaultValuePipe(1), ParseIntPipe) page = 1,
    @Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit = 10
  ) {
    return this.userService.findAll({ page, limit }, query);
  }

  @ApiOperation({ summary: '查询用户详情' })
  @ApiResponse({ status: 200, description: '200', type: User })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '查询用户详情')
  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.userService.findOne(id);
  }

  @ApiOperation({ summary: '修改用户' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '修改用户')
  @Patch()
  update(@Body() updateUserDto: UpdateUserDto, @Req() req: any) {
    return this.userService.update(updateUserDto, req.user.userId);
  }

  @ApiOperation({ summary: '删除用户' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '删除用户')
  @Delete(':id')
  delete(@Param('id', ParseIntPipe) id: number) {
    return this.userService.remove(id);
  }

  @ApiOperation({ summary: '修改密码' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '修改密码')
  @Patch('password')
  async updatePassword(@Body() updateManagerDto: UpdatePasswordUserDto) {
    return this.userService.updatePassword(updateManagerDto);
  }

  @ApiOperation({ summary: '重置密码' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '重置密码')
  @Patch('resetPassword/:id')
  resetPassword(@Param('id', ParseIntPipe) id: number) {
    return this.userService.resetPassword(id);
  }

  @ApiOperation({ summary: '设置管理员' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '设置管理员')
  @Patch('setAdmin/:id/:isAdmin')
  setAdmin(
    @Param('id', ParseIntPipe) id: number,
    @Param('isAdmin', ParseIntPipe) isAdmin: number
  ) {
    return this.userService.setAdmin(id, isAdmin);
  }

  @ApiOperation({ summary: '修改用户头像' })
  @SetMetadata(ReflectMetadataKeys.ACTION_NAME, '修改用户头像')
  @Patch('avatar')
  updateAvatar(
    @Req() req: any,
    @Body('avatar') avatar: Attachement
  ) {
    return this.userService.updateAvatar(req.user.userId, avatar);
  }
}

